<?php

namespace app\admin\controller\Count;

use app\common\controller\Backend;
use app\common\model\sales\Slip;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $goodsnum = Db::name('goods')->count();
        $slipsnum = Db::name('sales_slip')->count();
        $ordersnum = Db::name('purchase_order')->count();
        $this->view->assign([
            'goodsnum'        => $goodsnum,
            'slipsnum'       => $slipsnum,
            'ordersnum'       => $ordersnum,
            'totalorderamount' => 174800,
        ]);

        return $this->view->fetch();
    }
    public function getreport(){
        $days = input('times/d',7);
        $times = [];
        $datas1 = [];
        $datas2 = [];
        for($i=0;$i<$days;$i++){
            $times[] = date("Y-m-d",strtotime("-".$i." day"));
        }
        $list1 = Db::name('sales_slip')->where(array('createtime'=>array('>',time()-$days*86400),'status'=>array('<',6)))->select();
        $list2 = Db::name('purchase_order')->where(array('createtime'=>array('>',time()-$days*86400),'status'=>array('<',9)))->select();
        foreach ($times as $k => $v){
            $datas1[$k] =0;
            $datas2[$k] =0;
            foreach ($list1 as $key => $value){

                if($value['createtime'] > strtotime($v)){
                    $datas1[$k] += $value['actual_cost'];
                    unset($list1[$key]);
                }
            }
            foreach ($list2 as $key => $value){

                if($value['createtime'] > strtotime($v)){
                    $datas2[$k] += $value['order_price'];
                    unset($list2[$key]);
                }
            }
        }
        sort($times);
        $datas1 = array_reverse($datas1);
        $datas2 = array_reverse($datas2);

        $this->success('','',array(
            'times'=> $times,
            'datas1'=> $datas1,
            'datas2'=> $datas2,
        ));
    }
}
